Thumb

JavaScript While Loop

1/22/2020 6:19:40 AM

We know the for loop have three part declare, condition, and increment or decrement. Now while loop have same concept but the structure will be change. First, we need to declare the variable also assign the value the create “while” loop then under “()” symbol check the condition then under this “{}” bracket we use our expecting output then we write increment and decrement symbol. Then close the bracket, now given bellow the example code and explain the code:

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<script type="text/javascript">
  //decliear
var i = 0;
//condition
while (i < 10) {
  console.log("Number is " + i);
//increment
  i++;
}
</script>
</body>
</html>

First, we declare a variable name as “i” then initialize the variable then we write while loop and write the expected output and then write increment and decrement symbol.